{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 介绍：\n",
    "\n",
    "归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法（Divide and Conquer）的一个非常典型的应用。\n",
    "\n",
    "首先考虑下如何将将二个有序数列合并。这个非常简单，只要从比较二个数列的第一个数，谁小就先取谁，取了后就在对应数列中删除这个数。然后再进行比较，如果有数列为空，那直接将另一个数列的数据依次取出即可。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 原理\n",
    "\n",
    "归并（Merge）排序法是将两个（或两个以上）有序表合并成一个新的有序表，即把待排序序列分为若干个子序列，每个子序列是有序的。然后再把有序子序列合并为整体有序序列。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总结：\n",
    "\n",
    "    分治法思想，因为每次都拆开成2部分，所以效率是O(logN)。类似于二叉树。\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def merge_sort(nums):\n",
    "    n = len(nums)\n",
    "    if n <=1:\n",
    "        return nums\n",
    "    \n",
    "    mid = n // 2\n",
    "    print('nums ', nums)\n",
    "    print('nums[:mid]', nums[:mid])\n",
    "    left = merge_sort(nums[:mid])\n",
    "    print('left ', left)\n",
    "    \n",
    "    print('nums[mid:]', nums[mid:])\n",
    "    right = merge_sort(nums[mid:])\n",
    "    print('right ', right)\n",
    "    return merge(left, right)\n",
    "    \n",
    "def merge(left, right):\n",
    "    res = []\n",
    "    i = j = 0\n",
    "    while i < len(left) and j < len(right):\n",
    "        if left[i] < right[j]:\n",
    "            res.append(left[i])\n",
    "            i += 1\n",
    "        else:\n",
    "            res.append(right[j])\n",
    "            j += 1\n",
    "    res = res + left[i:] + right[j:]\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "nums = [1,4,8,3,7,2,5,9,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nums  [1, 4, 8, 3, 7, 2, 5, 9, 6]\n",
      "nums[:mid] [1, 4, 8, 3]\n",
      "nums  [1, 4, 8, 3]\n",
      "nums[:mid] [1, 4]\n",
      "nums  [1, 4]\n",
      "nums[:mid] [1]\n",
      "left  [1]\n",
      "nums[mid:] [4]\n",
      "right  [4]\n",
      "left  [1, 4]\n",
      "nums[mid:] [8, 3]\n",
      "nums  [8, 3]\n",
      "nums[:mid] [8]\n",
      "left  [8]\n",
      "nums[mid:] [3]\n",
      "right  [3]\n",
      "right  [3, 8]\n",
      "left  [1, 3, 4, 8]\n",
      "nums[mid:] [7, 2, 5, 9, 6]\n",
      "nums  [7, 2, 5, 9, 6]\n",
      "nums[:mid] [7, 2]\n",
      "nums  [7, 2]\n",
      "nums[:mid] [7]\n",
      "left  [7]\n",
      "nums[mid:] [2]\n",
      "right  [2]\n",
      "left  [2, 7]\n",
      "nums[mid:] [5, 9, 6]\n",
      "nums  [5, 9, 6]\n",
      "nums[:mid] [5]\n",
      "left  [5]\n",
      "nums[mid:] [9, 6]\n",
      "nums  [9, 6]\n",
      "nums[:mid] [9]\n",
      "left  [9]\n",
      "nums[mid:] [6]\n",
      "right  [6]\n",
      "right  [6, 9]\n",
      "right  [5, 6, 9]\n",
      "right  [2, 5, 6, 7, 9]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "merge_sort(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
